import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.sun.javafx.collections.SetAdapterChange;

public class SingleTalk extends JFrame 
{
	//�������
	private static JPanel NorthPanel;
	//�ϲ����
	private static JPanel SouthPanel;
	//�ָ����
	private static JSplitPane jsp;
	//��Ϣ��
	private static JTextArea Message_text;
	//������Ϣ��
	private static JTextField Send_text;
	private static JButton btn_sendMessage;
	//�������
	private static JScrollPane jscroll;
	//���ͻ��˵�socket
	private static Socket socket;
	//���ͻ��˵Ķ˿ں�
	private static int ID;
	//���ѵĶ˿ں�
	private static int port;

	
   public SingleTalk(Socket socket,int ID,int port)
   {
	   setTitle("�����˿ڣ�"+ID+" ���Ѷ˿ڣ�"+port);
	   //���ڴ�С
	   setSize(400,400);
	   //���ھ���
	   setLocationRelativeTo(null);
	   //�رմ��������ٵ�ǰ����
	   setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	   //���ò��ֱ߽粼��
	   setLayout(new BorderLayout());
	   //���ô��ڿɼ�
	   setVisible(true);
	   
	   //��ʼ���ָ����
	   jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
	   jsp.setDividerLocation(290);
	   
	   //��Ϣ��ʾ��
	   NorthPanel = new JPanel();
	   NorthPanel.setBorder(new TitledBorder("������Ϣ"));
	   NorthPanel.setLayout(new BorderLayout());
	   Message_text = new JTextArea();
	   jscroll = new JScrollPane(Message_text);
	   NorthPanel.add(jscroll,BorderLayout.CENTER);
	   jsp.setTopComponent(NorthPanel);
	   
	   //������
	   SouthPanel = new JPanel();
	   SouthPanel.setLayout(new BoxLayout(SouthPanel,BoxLayout.X_AXIS));
	   SouthPanel.setBorder(new TitledBorder("������'"));
	   Send_text = new JTextField(15);
	   btn_sendMessage = new JButton("����");
	   SouthPanel.add(Send_text);
	   SouthPanel.add(btn_sendMessage);
	   jsp.setBottomComponent(SouthPanel);
	   add(jsp,BorderLayout.CENTER);
	   
	   //��ʼ��socket,ID,port
	   this.socket = socket;
	   this.ID = ID;
	   this.port = port;
	   
	   //���������¼�
	   addWindowListener(new WindowListener() {
		
		@Override
		public void windowOpened(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			
		}
		
		@Override
		public void windowIconified(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			
		}
		
		@Override
		public void windowDeiconified(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			
		}
		
		@Override
		public void windowDeactivated(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			
		}
		
		@Override
		public void windowClosing(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}
		
		@Override
		public void windowClosed(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			
		}
		
		@Override
		public void windowActivated(WindowEvent arg0) {
			// TODO �Զ����ɵķ������
			
		}
	});
	   
   }
   
   public static void main(String[] args)
   {
	   
   }
   
   public static void startTalk()
   {
	   btn_sendMessage.addActionListener(new ActionListener() 
	   {
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if (!Send_text.getText().equals("")) 
			{
				try {
					PrintWriter pw = getWriter(socket);
					pw.println("#01" + ID + "-" + port + "-"
							+ Send_text.getText());

					//��ȡ��ǰϵͳʱ��
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					Message_text.append(df.format(new Date()) + "-������"
							+ Send_text.getText() + "\n");
					Send_text.setText("");

				} catch (IOException e) {
					// TODO �Զ����ɵ� catch ��
					e.printStackTrace();
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null, "���Ϳ���Ϊ��","��ʾ",JOptionPane.WARNING_MESSAGE);
			}
		}
	   });
   }
   
   private static PrintWriter getWriter(Socket socket)throws IOException//��ȡ�������������
   {
  	 OutputStream socketOut=socket.getOutputStream();
  	 return new PrintWriter(socketOut,true);
   }
   
   private  static BufferedReader getReader(Socket socket)throws IOException//��ȡ��������������
   {
  	 InputStream sokcetIn=socket.getInputStream();
  	 return new BufferedReader(new InputStreamReader(sokcetIn));
   }
   
   public static void receiver()
   {
	   try
	   {
		    BufferedReader br = getReader(socket);
		    String message=null;
	    	 while((message=br.readLine())!=null)//readLine()�����������������ݽ���ʱ��ִ����һ�����
	    	 {
	    		 //��Ϣ��ǰ�����ַ���"#01" �������������Ϣ
	    		if(message.substring(0, 3).equals("#01"))
	    		{
	    			getMessageFromFriend(message.substring(3));
	    		}
	    	 }
	   }
	   catch (IOException e)
	   {
		// TODO �Զ����ɵ� catch ��
		e.printStackTrace();
	   }
   }
   
   public static void getMessageFromFriend(String message)
   {
	   StringTokenizer st = new StringTokenizer(message, "-");
	   int srcID = Integer.parseInt(st.nextToken());
	   String destID = st.nextToken();
	   String content = st.nextToken();
	   
	 //��ȡ��ǰϵͳʱ��
//	   System.out.println("port:  "+port);
//	   System.out.println("srcID��  "+srcID);
	   if(port == srcID)
	   {
		   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   Message_text.append(df.format(new Date())+"-����-"+srcID+": "+content+"\n");
	   }
   }
   
   
}
